<?php

namespace App\Remote\XiaoHongShu;

use App\Exceptions\BaseException;
use GuzzleHttp\Exception\GuzzleException;
use Symfony\Component\HttpFoundation\Request as RequestAlias;

class XhsAuthRemote extends Remote
{
    /**
     * @inheritDoc
     */
    static function getBaseUri(): string
    {
        // TODO: Implement getBaseUri() method.
        return "http://xhs.pl666.cn";
    }

    /**
     * 构建公共请求头
     *
     * @return array
     */
    public static function buildBasicHeaders(): array
    {

        return [
            'Content-Type' => "application/json",
        ]; // TODO: Change the autogenerated stub
    }

    /**
     * @return array
     * @throws BaseException
     * @throws GuzzleException
     */
    public static function getToken(): array
    {
        $timestamp  = time();
        $merchantId = config('xhs.merchantId');
        $secret     = config('xhs.secret');
        return self::customRequestV3(
            "api/locallife/getToken",
            RequestAlias::METHOD_POST,
            [
                "sellerId" => $merchantId
            ],
            [
                "timestamp" => $timestamp,
                "sign"      => self::getSign($timestamp, $merchantId, $secret)
            ]
        )["data"];
    }

    /**
     * 获取小红书签名
     *
     * @param int $timestamp
     * @param string $merchantId
     * @param string $secret
     * @return string
     */
    public static function getSign(int $timestamp, string $merchantId, string $secret): string
    {
        return md5(vsprintf("%s%s%s", [
            $merchantId,
            $timestamp,
            $secret
        ]));
    }
}
